<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/19 01:00
 */

namespace module\user\model;

use mof\Model;
use think\model\relation\BelongsTo;

class ScoreLog extends Model
{
    protected $name       = 'user_score_log';
    protected $updateTime = false;

    protected $append = [
        'score_label', 'flow_label'
    ];

    protected array $searchFields = [
        'user_id'   => 'integer',
        'flow'      => 'string',
        'create_at' => ['type' => 'datetime', 'op' => 'between']
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    protected function getScoreLabelAttr($value, $data): string
    {
        return $data['flow'] === 'in' ? '+' . $data['score'] : '-' . $data['score'];
    }

    protected function getFlowLabelAttr($value, $data): string
    {
        $flow = $data['flow'];
        return $flow === 'in' ? '收入' : '支出';
    }
}